<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      v-show="showSearch"
      :inline="true"
    >
      <el-form-item label="订单号" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入订单号"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单状态" prop="orderStatues">
        <el-select
          v-model="queryParams.orderStatues"
          placeholder="请选择订单状态"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in statusDictDatas"
            :key="parseInt(dict.value)"
            :label="dict.label"
            :value="parseInt(dict.value)"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="订单时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          style="width: 240px"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="['00:00:00', '23:59:59']"
        />
      </el-form-item>

      <el-form-item label="订单来源" prop="createTime">
        <el-select
          v-model="queryParams.appId"
          placeholder="请选择"
          clearable
          @change="handleChange"
        >
          <el-option
            v-for="item in options"
            :key="item.appId"
            :label="item.appName"
            :value="item.appId"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="roleList" border>
      <el-table-column label="用户id" align="center" prop="buyerId" />
      <el-table-column
        label="订单号"
        align="center"
        prop="outTradeNo"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="宝宝姓氏" align="center" prop="familyName" />
      <el-table-column label="出生日期" align="center" prop="solarDate" />
      <el-table-column label="原价" align="center" prop="costPrise" />
      <el-table-column label="订单金额" align="center" prop="retailPrise" />
      <el-table-column label="订单来源" align="center" prop="appName" />
      <el-table-column label="渠道标识" align="center" prop="appId" />
      <el-table-column label="订单状态" align="center" prop="orderStatues">
        <template v-slot="scope">
          <dict-tag
            :type="DICT_TYPE.CMS_BBNAMES_ORDERS_STATE"
            :value="scope.row.orderStatues"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="订单创建时间"
        align="center"
        prop="orderTime"
        class-name="small-padding fixed-width"
      >
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.orderTime) }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { getBbNamesOrders } from "@/api/distribute/order";
import { getUserProfile } from "@/api/system/user";
import { getInfoAuthAliAppPage } from "@/api/alipay/infoAuthAliApp";
import { getDictDatas, DICT_TYPE } from "@/utils/dict";

export default {
  name: "NamesIndex",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 查询参数
      queryParams: {
        creator: null,
        pageNo: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      defaultProps: {
        label: "name",
        children: "children",
      },
      roleList: [],
      options: [],

      value: "",
      statusDictDatas: getDictDatas(DICT_TYPE.CMS_BBNAMES_ORDERS_STATE),
    };
  },
  created() {
    this.getUserInfo();
    this.getAppletList();
  },
  methods: {
    /** 查询订单列表 */
    getList() {
      this.loading = true;
      getBbNamesOrders(this.queryParams).then((response) => {
        this.roleList = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 获取用户信息
    getUserInfo() {
      const that = this;
      getUserProfile().then((response) => {
        this.queryParams.creator = response.data.id;
        if (that.queryParams.creator) {
          this.getList();
        }
      });
    },
    // 查询小程序
    getAppletList() {
      const queryParams = {
        pageNo: 1,
        pageSize: 100,
      };
      // 执行查询
      getInfoAuthAliAppPage(queryParams).then((response) => {
        this.options = response.data.list;
        // this.queryParams.appId = response.data.list[0].appId;
        // console.log("response.data.list;", response.data.list[0]);
        this.getList();
      });
    },
    // 选择小程序
    handleChange(value) {
      // 当选择项发生变化时，这个方法会被调用
      console.log("当前选择的值：", value);
      // this.queryParams.appId = value;
      // 在这里执行你的业务逻辑
      this.getList();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 取消按钮（数据权限）
    cancelDataScope() {
      this.openDataScope = false;
      this.reset();
    },
    // 取消按钮（菜单权限）
    cancelMenu() {
      this.openMenu = false;
      this.reset();
    },
    // 表单重置
    reset() {
      if (this.$refs.menu !== undefined) {
        this.$refs.menu.setCheckedKeys([]);
      }
      this.menuExpand = false;
      this.menuNodeAll = false;
      this.deptExpand = true;
      this.deptNodeAll = false;
      this.form = {
        id: undefined,
        name: undefined,
        code: undefined,
        sort: 0,
        deptIds: [],
        menuIds: [],
        dataScope: undefined,
        deptCheckStrictly: false,
        menuCheckStrictly: true,
        remark: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
  },
};
</script>
